#include<iostream>
#include<string>
using namespace std;


class Solution {
public:
    string reverseWords(string s) {
        string st;
        string ans;
        int len = s.size();
        for (int i = len - 1; i >= 0; i--) {
            if (s[i] == ' ') continue;
            while (i >= 0 && s[i] != ' ') {
                st.push_back(s[i]);
                i--;
            }
            reverse(st.begin(), st.end());
            ans += st;
            ans += ' ';
            st.clear();
        }
        ans.pop_back();
        return ans;
    }
};